package bb.lanxing.util.ui

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import java.lang.reflect.Type

class GsonUtils {
    val gson: Gson = Gson()

    fun <T> toJson(t: T): String {
        return gson.toJson(t)
    }

    fun <T> fromJson(jsonString: String?, clazz: Class<T>?): T {
        return gson.fromJson(jsonString, clazz) as T
    }

    fun <T> fromJson(jsonString: String?, typeOfT: Type?): T {
        return gson.fromJson(jsonString, typeOfT)
    }

    fun <T> fromJsonList(jsonString: String?): List<T> {
        return gson.fromJson(jsonString, object : TypeToken<List<T>?>() {}.type)
    }

    fun <T> fromJsonList(jsonString: String?, typeOfT: Type?): List<T> {
        return gson.fromJson(jsonString, typeOfT)
    }

    companion object {
        private val gsonUtils = GsonUtils()
        fun getInstance(): GsonUtils {
            return gsonUtils
        }
    }
}
